const CourseService = require('../services/CourseService')

const CourseController = {
  add: async (req, res) => {
    await CourseService.add(req.body)
    res.send({
      ActionType: 'OK'
    })
  },

  getList: async (req, res) => {
    const page = +req.query.page || 1
    const limit = +req.query.limit || 5
    const result = await CourseService.getList({ _id: req.params.id }, req.query, page, limit)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getListPublished: async (req, res) => {
    const result = await CourseService.getListPublished({ ispublish: 1 })
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getMyList: async (req, res) => {
    const page = +req.query.page || 1
    const limit = +req.query.limit || 5
    const result = await CourseService.getMyList({ courseteacher: req.params.username }, req.query, page, limit)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getMyListPublished: async (req, res) => {
    const result = await CourseService.getMyListPublished({ courseteacher: req.params.username, ispublish: 1 })
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getStudentList: async (req, res) => {
    const result = await CourseService.getStudentList(req.params)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getStudentPublishedList: async (req, res) => {
    const result = await CourseService.getStudentPublishedList(req.params)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  putList: async (req, res) => {
    // console.log(req.body)
    const result = await CourseService.putList({
      _id: req.params.id,
      courseid: Number(req.body.courseid),
      coursename: req.body.coursename,
      courseteacher: Number(req.body.courseteacher),
      major: req.body.major,
      classes: Number(req.body.classes),
      term: Number(req.body.term),
      credit: Number(req.body.credit),
      type: Number(req.body.type),
      ratio: req.body.ratio
    })
    res.send({
      ActionType: 'OK'
    })
  },

  delList: async (req, res) => {
    await CourseService.delList({ courseid: req.params.courseid })
    res.send({
      ActionType: 'OK'
    })
  },

  putPublish: async (req, res) => {
    // console.log(typeof req.body.ispublish)
    // console.log(req.params.courseid)
    await CourseService.putPublish(req.params.courseid, req.body)
    res.send({
      ActionType: 'OK'
    })
  },

  getTotal: async (req, res) => {
    const result = await CourseService.getTotal(req.query)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getMyTotal: async (req, res) => {
    const result = await CourseService.getMyTotal({ courseteacher: req.params.username }, req.query)
    res.send({
      ActionType: 'OK',
      data: result
    })
  }
}

module.exports = CourseController
